home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / screen32.lha / screen-3.2b / getloadavg.c < prev    next >
C/C++ Source or Header  |  1992-10-27  |  15KB  |  629 lines

  1. /* Get the system load averages.
  2.    Copyright (C) 1985, 86, 87, 88, 89, 91, 92 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Compile-time symbols that this file uses:
  19.  
  20.    FIXUP_KERNEL_SYMBOL_ADDR()    Adjust address in returned struct nlist.
  21.    KERNEL_FILE            Pathname of the kernel to nlist.
  22.    LDAV_CVT()            Scale the load average from the kernel.
  23.                 Returns a double.
  24.    LDAV_SYMBOL            Name of kernel symbol giving load average.
  25.    LOAD_AVE_TYPE        Type of the load average array in the kernel.
  26.                 Must be defined unless one of
  27.                 apollo, DGUX, NeXT, or UMAX is defined;
  28.                 otherwise, no load average is available.
  29.    NLIST_STRUCT            Include nlist.h, not a.out.h, and
  30.                 the nlist n_name element is a pointer,
  31.                 not an array.
  32.    NLIST_NAME_UNION        struct nlist has an n_un member, not n_name.
  33.  
  34.    Specific system predefines this file uses, aside from setting
  35.    default values if not emacs:
  36.  
  37.    apollo
  38.    BSD                Real BSD, not just BSD-like.
  39.    DGUX
  40.    eunice            UNIX emulator under VMS.
  41.    NeXT
  42.    sgi
  43.    sony_news                    NEWS-OS (works at least for 4.1C)
  44.    UMAX
  45.    UMAX4_3
  46.    VMS
  47.  
  48.    In addition, to avoid nesting many #ifdefs, we internally set
  49.    LDAV_DONE to indicate that the load average has been computed.  */
  50.  
  51. #ifdef emacs
  52. #include "config.h"
  53.  
  54. /* The existing Emacs configuration files define a macro called
  55.    LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
  56.    returns the load average multiplied by 100.  What we actually want
  57.    is a macro called LDAV_CVT, which returns the load average as an
  58.    unmultiplied double.
  59.  
  60.    For backwards compatibility, we'll define LDAV_CVT in terms of
  61.    LOAD_AVE_CVT, but future machine config files should just define
  62.    LDAV_CVT directly.  */
  63.  
  64. #if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT)
  65. #define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
  66. #endif
  67.  
  68. #else /* not emacs */
  69.  
  70. #ifdef unix
  71. #include <sys/param.h>
  72. #endif
  73.  
  74. #ifdef NeXT
  75. /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
  76.    conflicts with the definition understood in this file, that this
  77.    really is BSD. */
  78. #undef BSD
  79. #endif
  80.  
  81. /* Set values that are different from the defaults, which are
  82.    set a little farther down with #ifndef.  */
  83.  
  84.  
  85. /* Some shorthands.  */
  86. #if defined(hp300) && !defined(hpux)
  87. #define MORE_BSD
  88. #endif
  89.  
  90. #if defined(ultrix) && defined(mips)
  91. #define decstation
  92. #endif
  93.  
  94. #if defined(sun) && defined(SVR4)
  95. #define SUNOS_5
  96. #endif
  97.  
  98.  
  99. #if defined(MORE_BSD) || defined(sun) || defined(decstation) || defined(_SEQUENT_) || defined(sgi) || defined(SVR4) || defined(sony_news)
  100. #define LOAD_AVE_TYPE long
  101. #endif
  102.  
  103.  
  104. #ifndef    FSCALE
  105.  
  106. /* SunOS and some others define FSCALE in sys/param.h.  */
  107.  
  108. #ifdef MORE_BSD
  109. #define FSCALE 2048.0
  110. #endif
  111.  
  112. #if defined(MIPS) || defined(SVR4) || defined(decstation)
  113. #define FSCALE 256
  114. #endif
  115.  
  116. #ifdef sgi
  117. #define    FSCALE 1000.0
  118. #endif
  119.  
  120. #ifdef FSCALE
  121. #define    LDAV_CVT(n) (((double) (n)) / FSCALE)
  122. #endif
  123.  
  124. #endif    /* Not FSCALE.  */
  125.  
  126.  
  127. #if !defined(NLIST_STRUCT) && (defined(MORE_BSD) || defined(sun) || defined(decstation) || defined(hpux) || defined(_SEQUENT_) || defined(sequent) || defined(sgi) || defined(SVR4)) || defined(sony_news)
  128. #define NLIST_STRUCT
  129. #endif
  130.  
  131.  
  132. #if defined(sgi) || (defined(mips) && !defined(BSD))
  133. #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
  134. #endif
  135.  
  136.  
  137. #ifdef sequent
  138. #define KERNEL_FILE "/dynix"
  139. #endif
  140.  
  141. #ifdef hpux
  142. #define KERNEL_FILE "/hp-ux"
  143. #endif
  144.  
  145. #if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4))
  146. #define KERNEL_FILE "/unix"
  147. #endif
  148.  
  149.  
  150. #ifdef alliant
  151. #define LDAV_SYMBOL "_Loadavg"
  152. #endif
  153.  
  154. #if !defined(LDAV_SYMBOL) && (defined(hpux) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi))
  155. #define LDAV_SYMBOL "avenrun"
  156. #endif
  157.  
  158. #endif /* not emacs */
  159.  
  160. #ifdef HAVE_UNISTD_H
  161. #include <unistd.h>
  162. #endif
  163.  
  164. #if defined(USG) || defined(SYSV) || defined(_POSIX_VERSION)
  165. #include <fcntl.h>
  166. #else
  167. #include <sys/file.h>
  168. #endif
  169.  
  170. #ifdef TEST
  171. #include <stdio.h>
  172. #include <errno.h>
  173.  
  174. #ifndef errno
  175. extern int errno;
  176. #endif
  177. #endif /* TEST */
  178.  
  179. /* LOAD_AVE_TYPE should only get defined if we're going to use the
  180.    nlist method.  */
  181. #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL))
  182. #define LOAD_AVE_TYPE double
  183. #endif
  184.  
  185. #ifdef LOAD_AVE_TYPE
  186.  
  187. #ifndef VMS
  188. #ifndef NLIST_STRUCT
  189. #include <a.out.h>
  190. #else /* NLIST_STRUCT */
  191. #include <nlist.h>
  192. #endif /* NLIST_STRUCT */
  193.  
  194. #ifdef SUNOS_5
  195. #include <fcntl.h>
  196. #include <kvm.h>
  197. #endif
  198.  
  199. #ifndef KERNEL_FILE
  200. #define KERNEL_FILE "/vmunix"
  201. #endif /* KERNEL_FILE */
  202.  
  203. #ifndef LDAV_SYMBOL
  204. #define LDAV_SYMBOL "_avenrun"
  205. #endif /* LDAV_SYMBOL */
  206.  
  207. #else /* VMS */
  208.  
  209. #ifndef eunice
  210. #include <iodef.h>
  211. #include <descrip.h>
  212. #else /* eunice */
  213. #include <vms/iodef.h>
  214. #endif /* eunice */
  215. #endif /* VMS */
  216.  
  217. #ifndef LDAV_CVT
  218. #define LDAV_CVT(n) ((double) (n))
  219. #endif /* !LDAV_CVT */
  220.  
  221. #endif /* LOAD_AVE_TYPE */
  222.  
  223. #ifdef NeXT
  224. #include <mach.h>
  225. #endif /* NeXT */
  226.  
  227. #ifdef sgi
  228. #include <sys/types.h>
  229. #include <sys/sysmp.h>
  230. #endif /* sgi */
  231.  
  232. #ifdef UMAX
  233. #include <stdio.h>
  234. #include <signal.h>
  235. #include <sys/time.h>
  236. #include <sys/types.h>
  237. #include <sys/wait.h>
  238. #include <sys/syscall.h>
  239.  
  240. #ifdef UMAX_43
  241. #include <machine/cpu.h>
  242. #include <inq_stats/statistics.h>
  243. #include <inq_stats/sysstats.h>
  244. #include <inq_stats/cpustats.h>
  245. #include <inq_stats/procstats.h>
  246. #else /* Not UMAX_43.  */
  247. #include <sys/sysdefs.h>
  248. #include <sys/statistics.h>
  249. #include <sys/sysstats.h>
  250. #include <sys/cpudefs.h>
  251. #include <sys/cpustats.h>
  252. #include <sys/procstats.h>
  253. #endif /* Not UMAX_43.  */
  254. #endif /* UMAX */
  255.  
  256. #ifdef DGUX
  257. #include <sys/dg_sys_info.h>
  258. #endif
  259.  
  260. /* Avoid static vars inside a function since in HPUX they dump as pure.  */
  261.  
  262. #ifdef NeXT
  263. static processor_set_t default_set;
  264. static int initialized;
  265. #endif /* NeXT */
  266.  
  267. #ifdef UMAX
  268. static unsigned int cpus = 0;
  269. static unsigned int samples;
  270. #endif /* UMAX */
  271.  
  272. #ifdef DGUX
  273. static struct dg_sys_info_load_info load_info;    /* what-a-mouthful! */
  274. #endif /* DGUX */
  275.  
  276. #ifdef LOAD_AVE_TYPE
  277. /* File descriptor open to /dev/kmem or VMS load ave driver.  */
  278. static int channel;
  279. /* Nonzero iff channel is valid.  */
  280. static int initialized;
  281. /* Offset in kmem to seek to read load average, or 0 means invalid.  */
  282. static long offset;
  283.  
  284. #if !defined(VMS) && !defined(sgi)
  285. static struct nlist nl[2];
  286. #endif /* Not VMS or sgi */
  287.  
  288. #ifdef SUNOS_5
  289. static kvm_t *kd;
  290. #endif /* SUNOS_5 */
  291.  
  292. #endif /* LOAD_AVE_TYPE */
  293.  
  294. /* Put the 1 minute, 5 minute and 15 minute load averages
  295.    into the first NELEM elements of LOADAVG.
  296.    Return the number written (never more than 3),
  297.    or -1 if an error occurred.  */
  298.  
  299. int
  300. getloadavg (loadavg, nelem)
  301.      double loadavg[];
  302.      int nelem;
  303. {
  304.   int elem = 0;            /* Return value.  */
  305.  
  306. #if defined(NeXT)
  307. #define LDAV_DONE
  308.   /* The NeXT code was adapted from iscreen 3.2.
  309.      We only know how to get the 1-minute average for this system.  */
  310.  
  311.   host_t host;
  312.   struct processor_set_basic_info info;
  313.   unsigned info_count;
  314.  
  315.   if (!initialized)
  316.     {
  317.       if (processor_set_default (host_self (), &default_set) == KERN_SUCCESS)
  318.     initialized = 1;
  319.     }
  320.  
  321.   if (initialized)
  322.     {
  323.       info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
  324.       if (processor_set_info (default_set, PROCESSOR_SET_BASIC_INFO, &host,
  325.                  (processor_set_info_t) &info, &info_count)
  326.       != KERN_SUCCESS)
  327.     initialized = 0;
  328.       else
  329.     {
  330.       if (nelem > 0)
  331.         loadavg[elem++] = (double) info.load_average / LOAD_SCALE;
  332.     }
  333.     }
  334.  
  335.   if (!initialized)
  336.     return -1;
  337. #endif /* NeXT */
  338.  
  339. #if defined(UMAX)
  340. #define LDAV_DONE
  341. /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
  342.    have a /dev/kmem.  Information about the workings of the running kernel
  343.    can be gathered with inq_stats system calls.
  344.    We only know how to get the 1-minute average for this system.  */
  345.  
  346.   struct proc_summary proc_sum_data;
  347.   struct stat_descr proc_info;
  348.   double load;
  349.   register unsigned int i, j;
  350.  
  351.   if (cpus == 0)
  352.     {
  353.       register unsigned int c, i;
  354.       struct cpu_config conf;
  355.       struct stat_descr desc;
  356.  
  357.       desc.sd_next = 0;
  358.       desc.sd_subsys = SUBSYS_CPU;
  359.       desc.sd_type = CPUTYPE_CONFIG;
  360.       desc.sd_addr = (char *) &conf;
  361.       desc.sd_size = sizeof conf;
  362.  
  363.       if (inq_stats (1, &desc))
  364.     return -1;
  365.  
  366.       c = 0;
  367.       for (i = 0; i < conf.config_maxclass; ++i)
  368.     {
  369.       struct class_stats stats;
  370.       bzero ((char *) &stats, sizeof stats);
  371.  
  372.       desc.sd_type = CPUTYPE_CLASS;
  373.       desc.sd_objid = i;
  374.       desc.sd_addr = (char *) &stats;
  375.       desc.sd_size = sizeof stats;
  376.  
  377.       if (inq_stats (1, &desc))
  378.         return -1;
  379.  
  380.       c += stats.class_numcpus;
  381.     }
  382.       cpus = c;
  383.       samples = cpus < 2 ? 3 : (2 * cpus / 3);
  384.     }
  385.  
  386.   proc_info.sd_next = 0;
  387.   proc_info.sd_subsys = SUBSYS_PROC;
  388.   proc_info.sd_type = PROCTYPE_SUMMARY;
  389.   proc_info.sd_addr = (char *) &proc_sum_data;
  390.   proc_info.sd_size = sizeof (struct proc_summary);
  391.   proc_info.sd_sizeused = 0;
  392.  
  393.   if (inq_stats (1, &proc_info) != 0)
  394.     return -1;
  395.  
  396.   load = proc_sum_data.ps_nrunnable;
  397.   j = 0;
  398.   for (i = samples - 1; i > 0; --i)
  399.     {
  400.       load += proc_sum_data.ps_nrun[j];
  401.       if (j++ == PS_NRUNSIZE)
  402.     j = 0;
  403.     }
  404.  
  405.   if (nelem > 0)
  406.     loadavg[elem++] = load / samples / cpus;
  407. #endif /* UMAX */
  408.  
  409. #if defined(DGUX)
  410. #define LDAV_DONE
  411.   /* This call can return -1 for an error, but with good args
  412.      it's not supposed to fail.  The first argument is for no
  413.      apparent reason of type `long int *'.  */
  414.   dg_sys_info ((long int *) &load_info,
  415.            DG_SYS_INFO_LOAD_INFO_TYPE,
  416.            DG_SYS_INFO_LOAD_VERSION_0);
  417.  
  418.   if (nelem > 0)
  419.     loadavg[elem++] = load_info.one_minute;
  420.   if (nelem > 1)
  421.     loadavg[elem++] = load_info.five_minute;
  422.   if (nelem > 2)
  423.     loadavg[elem++] = load_info.fifteen_minute;
  424. #endif /* DGUX */
  425.  
  426. #ifdef apollo
  427. #define LDAV_DONE
  428. /* Apollo code from lisch@mentorg.com (Ray Lischner).
  429.  
  430.    This system call is not documented.  The load average is obtained as
  431.    three long integers, for the load average over the past minute,
  432.    five minutes, and fifteen minutes.  Each value is a scaled integer,
  433.    with 16 bits of integer part and 16 bits of fraction part.
  434.  
  435.    I'm not sure which operating system first supported this system call,
  436.    but I know that SR10.2 supports it.  */
  437.  
  438.   extern void proc1_$get_loadav ();
  439.   unsigned long load_ave[3];
  440.  
  441.   proc1_$get_loadav (load_ave);
  442.  
  443.   if (nelem > 0)
  444.     loadavg[elem++] = load_ave[0] / 65536.0;
  445.   if (nelem > 1)
  446.     loadavg[elem++] = load_ave[1] / 65536.0;
  447.   if (nelem > 2)
  448.     loadavg[elem++] = load_ave[2] / 65536.0;
  449. #endif /* apollo */
  450.  
  451. #if defined(VMS)
  452. #define LDAV_DONE
  453.   /* VMS specific code -- read from the Load Ave driver.  */
  454.  
  455.   LOAD_AVE_TYPE load_ave[3];
  456. #ifdef eunice
  457.   struct
  458.   {
  459.     int dsc$w_length;
  460.     char *dsc$a_pointer;
  461.   } descriptor;
  462. #endif
  463.  
  464.   /* Ensure that there is a channel open to the load ave device.  */
  465.   if (!initialized)
  466.     {
  467.       /* Attempt to open the channel.  */
  468. #ifdef eunice
  469.       descriptor.size = 18;
  470.       descriptor.ptr = "$$VMS_LOAD_AVERAGE";
  471. #else
  472.       $DESCRIPTOR (descriptor, "LAV0:");
  473. #endif
  474.       if (sys$assign (&descriptor, &channel, 0, 0) & 1)
  475.     initialized = 1;
  476.     }
  477.  
  478.   /* Read the load average vector.  */
  479.   if (initialized && !(sys$qiow (0, channel, IO$_READVBLK, 0, 0, 0,
  480.                  load_ave, 12, 0, 0, 0, 0) & 1))
  481.     {
  482.       sys$dassgn (channel);
  483.       initialized = 0;
  484.     }
  485.  
  486.   if (!initialized)
  487.     return -1;
  488. #endif /* VMS */
  489.  
  490. #if defined(LOAD_AVE_TYPE) && !defined(VMS)
  491. #define LDAV_DONE
  492.   /* UNIX-specific code -- read the average from /dev/kmem.  */
  493.  
  494.   LOAD_AVE_TYPE load_ave[3];
  495.  
  496.   /* Get the address of LDAV_SYMBOL.  */
  497.   if (offset == 0)
  498.     {
  499. #ifndef SUNOS_5
  500. #ifndef sgi
  501. #ifndef NLIST_STRUCT
  502.       strcpy (nl[0].n_name, LDAV_SYMBOL);
  503.       strcpy (nl[1].n_name, "");
  504. #else /* NLIST_STRUCT */
  505. #ifdef NLIST_NAME_UNION
  506.       nl[0].n_un.n_name = LDAV_SYMBOL;
  507.       nl[1].n_un.n_name = 0;
  508. #else /* not NLIST_NAME_UNION */
  509.       nl[0].n_name = LDAV_SYMBOL;
  510.       nl[1].n_name = 0;
  511. #endif /* not NLIST_NAME_UNION */
  512. #endif /* NLIST_STRUCT */
  513.  
  514.       if (nlist (KERNEL_FILE, nl) >= 0)
  515.     /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i.  */
  516.     {
  517. #ifdef FIXUP_KERNEL_SYMBOL_ADDR
  518.       FIXUP_KERNEL_SYMBOL_ADDR (nl);
  519. #endif
  520.       offset = nl[0].n_value;
  521.     }
  522. #else /* sgi */
  523.       int ldav_off;
  524.  
  525.       ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN);
  526.       if (ldav_off != -1)
  527.     offset = (long) ldav_off & 0x7fffffff;
  528. #endif /* sgi */
  529. #endif  /* !SUNOS_5 */
  530.     }
  531.  
  532.   /* Make sure we have /dev/kmem open.  */
  533.   if (!initialized)
  534.     {
  535. #ifndef SUNOS_5
  536.       channel = open ("/dev/kmem", 0);
  537.       if (channel >= 0)
  538.     initialized = 1;
  539. #else /* SUNOS_5 */
  540.       kd = kvm_open (0, 0, 0, O_RDONLY, 0);
  541.       if (kd != 0) 
  542.     {
  543.       kvm_nlist (kd, nl);
  544.       initialized = 1;
  545.     }
  546. #endif /* SUNOS_5 */
  547.     }
  548.  
  549.   /* If we can, get the load average values.  */
  550.   if (offset && initialized)
  551.     {
  552.       /* Try to read the load.  */
  553. #ifndef SUNOS_5
  554.       if (lseek (channel, offset, 0) == -1L
  555.       || read (channel, (char *) load_ave, sizeof (load_ave))
  556.       != sizeof (load_ave))
  557.     {
  558.       close (channel);
  559.       initialized = 0;
  560.     }
  561. #else  /* SUNOS_5 */
  562.       if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave))
  563.       != sizeof (load_ave))
  564.         {
  565.           kvm_close (kd);
  566.           initialized = 0;
  567.     }
  568. #endif /* SUNOS_5 */
  569.     }
  570.  
  571.   if (offset == 0 || !initialized)
  572.     return -1;
  573. #endif /* LOAD_AVE_TYPE and not VMS */
  574.  
  575. #ifdef LOAD_AVE_TYPE        /* Including VMS.  */
  576.   if (nelem > 0)
  577.     loadavg[elem++] = LDAV_CVT (load_ave[0]);
  578.   if (nelem > 1)
  579.     loadavg[elem++] = LDAV_CVT (load_ave[1]);
  580.   if (nelem > 2)
  581.     loadavg[elem++] = LDAV_CVT (load_ave[2]);
  582. #endif /* LOAD_AVE_TYPE */
  583.  
  584. #ifdef LDAV_DONE
  585.   return elem;
  586. #else
  587.   return -1;
  588. #endif
  589. }
  590.  
  591. #ifdef TEST
  592. void
  593. main (argc, argv)
  594.      int argc;
  595.      char **argv;
  596. {
  597.   int naptime = 0;
  598.  
  599.   if (argc > 1)
  600.     naptime = atoi (argv[1]);
  601.  
  602.   if (naptime == 0)
  603.     naptime = 5;
  604.  
  605.   while (1)
  606.     {
  607.       double avg[3];
  608.       int loads;
  609.  
  610.       errno = 0;        /* Don't be misled if it doesn't set errno.  */
  611.       loads = getloadavg (avg, 3);
  612.       if (loads == -1)
  613.     {
  614.       perror ("Error getting load average");
  615.       exit (1);
  616.     }
  617.       if (loads > 0)
  618.     printf ("1-minute: %f  ", avg[0]);
  619.       if (loads > 1)
  620.     printf ("5-minute: %f  ", avg[1]);
  621.       if (loads > 2)
  622.     printf ("15-minute: %f  ", avg[2]);
  623.       if (loads > 0)
  624.     putchar ('\n');
  625.       sleep (naptime);
  626.     }
  627. }
  628. #endif /* TEST */
  629.